package com.ruoyi.operation.order.product.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.operation.order.gift.domain.Gift;
import com.ruoyi.operation.order.gift.service.IGiftService;
import com.ruoyi.operation.order.product.domain.Product;
import com.ruoyi.operation.order.product.service.IProductService;
import com.ruoyi.operation.order.shop.domain.Shop;
import com.ruoyi.operation.order.shop.service.IShopService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import net.bytebuddy.agent.builder.AgentBuilder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author king
 * @create 2019/4/9 9:05
 */
@Controller
@RequestMapping(value = "/product/product")
public class ProductController extends BaseController {


    // 定义一个跳转到页面的路径
    private String prefix = "order/product/";


    @Autowired
    private IProductService iProductService;

    @Autowired
    private IGiftService iGiftService;

    @Autowired
    private IDeptService iDeptService;

    @Autowired
    private IShopService shopService;


    /**
     * 跳转到产品管理的信息列表
     * @return
     */
    @RequiresPermissions(value = "product:product:product")
    @GetMapping()
    String product(){
        return prefix + "product";
    }

    /**
     * 查询所有的产品管理的数据
     * @param product
     * @return
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions(value = "product:product:product")
    @ResponseBody
    public TableDataInfo list(Product product){
        // 调用分解的方法
        startPage();
        // 获取所有的产品的数据
        List<Product> list = iProductService.allProductList(product);
        return getDataTable(list);
    }

    /**
     * 跳转到产品新增的页面
     * @return
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions(value = "product:product:add")
    String add(){
        return prefix + "add";
    }


    /**
     * 新增产品信息
     * @param product
     * @return
     */
    @RequestMapping(value = "/saveProduct")
    @RequiresPermissions(value = "product:product:add")
    @ResponseBody
    public AjaxResult saveProduct(Product product){
        return toAjax(iProductService.saveProduct(product));
    }

    @RequestMapping(value = "/edit/{id}")
    @RequiresPermissions(value = "product:product:edit")
    String edit(@PathVariable("id") String id, Model model, Product product){
        // 获取所有的赠品
        List<Gift> allGift = iGiftService.allGiftForPro();
        List<Dept> allDept = iDeptService.allDeptList();
        if (null != id && id != ""){
            product = iProductService.getProductById(id);
        }
        List<Shop> allShop=shopService.selectByDeptId(product.getProductDeptId());
        model.addAttribute("allGift",allGift);
        model.addAttribute("allDept",allDept);
        model.addAttribute("product",product);
        model.addAttribute("allShop",allShop);
        return prefix + "edit";
    }


    /**
     * 编辑产品信息
     * @param product
     * @return
     */
    @RequestMapping(value = "editProduct")
    @ResponseBody
    @RequiresPermissions(value = "product:product:edit")
    public AjaxResult editProduct(Product product){

        return toAjax(iProductService.editProduct(product));
    }

    /**
     * 删除产品信息
     * @param id
     * @return
     */

    @RequiresPermissions(value = "product:product:remove")
    @RequestMapping(value = "/remove/{id}")
    @ResponseBody
    public AjaxResult removeProduct(@PathVariable("id") String id){
        return toAjax(iProductService.removeProduct(id));
    }





    /**
     * 查询所有的赠品信息做下拉框
     * @return
     */
    @RequestMapping(value = "/allGift")
    @ResponseBody
    public List<Gift> allGift(){
        return iGiftService.allGiftForPro();
    }

    /**
     * 查询所有的平台
     * @return
     */
    @RequestMapping(value = "/allDept")
    @ResponseBody
    public List<Dept> allDept(){
        return iDeptService.allDeptList();
    }

    /**
     * 根据选择的平台获取店铺
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectByDeptId")
    @ResponseBody
    public List<Shop> selectShopByDeptId(String id){
        return shopService.selectByDeptId(id);
    }





}
